package com.ruoyi.web.controller.biz;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.constant.ComplainConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Result;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ConvertUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.valid.ValidUpdateGroup;
import com.ruoyi.system.domain.BsComplain;
import com.ruoyi.system.domain.excel.BsComplainExcel;
import com.ruoyi.system.domain.excel.BsStrategyExcel;
import com.ruoyi.system.domain.ro.BsComplainHandleRO;
import com.ruoyi.system.domain.ro.BsComplainRO;
import com.ruoyi.system.service.IBsComplainService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 投诉信息
 *
 * @author ruoyi
 * @date 2023-11-20
 */
@Api(tags = "WEB端-投诉管理")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/complain")
public class BsComplainController extends BaseController {

    private final IBsComplainService iBsComplainService;

    @ApiOperation(value = "投诉管理-列表导出", tags = {"WEB端-投诉管理"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "投诉类型-见字典complain_type"),
            @ApiImplicitParam(name = "handleStatus", value = "处理-处理状态 0待处理 1需跟进 2已处理 详见字典complain_handle_status"),
            @ApiImplicitParam(name = "beginCreateTime", value = "投诉时间-start"),
            @ApiImplicitParam(name = "endCreateTime", value = "投诉时间-end"),
            @ApiImplicitParam(name = "pageNum", value = "分页参数：第几页"),
            @ApiImplicitParam(name = "pageSize", value = "分页参数：每页条数"),
    })
    @GetMapping("/export")
    public AjaxResult<?> export(@RequestParam(required = false) String type, @RequestParam(required = false) String handleStatus,
                                @RequestParam(required = false) String beginCreateTime,
                                @RequestParam(required = false) String endCreateTime) {
        BsComplainRO ro = BsComplainRO.builder().type(type).handleStatus(handleStatus).build();
        HashMap<String, Object> param = new HashMap<>(2);
        param.put("beginCreateTime", beginCreateTime);
        param.put("endCreateTime", endCreateTime);
        ro.setParams(param);
        List<BsComplain> list = iBsComplainService.queryList(ro);
        ExcelUtil<BsComplainExcel> util = new ExcelUtil<>(BsComplainExcel.class);
        return util.exportExcel(ConvertUtil.sourceToTarget(list, BsComplainExcel.class), "投诉反馈");
    }

    @ApiOperation(value = "投诉管理-列表", tags = {"WEB端-投诉管理"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "投诉类型-见字典complain_type"),
            @ApiImplicitParam(name = "handleStatus", value = "处理-处理状态 0待处理 1需跟进 2已处理 详见字典complain_handle_status"),
            @ApiImplicitParam(name = "beginCreateTime", value = "投诉时间-start"),
            @ApiImplicitParam(name = "endCreateTime", value = "投诉时间-end"),
            @ApiImplicitParam(name = "pageNum", value = "分页参数：第几页"),
            @ApiImplicitParam(name = "pageSize", value = "分页参数：每页条数"),
    })
    @GetMapping("/list")
    public Result<TableDataInfo<BsComplain>> list(@RequestParam(required = false) String type, @RequestParam(required = false) String handleStatus,
                                                  @RequestParam(required = false) String beginCreateTime,
                                                  @RequestParam(required = false) String endCreateTime) {
        BsComplainRO ro = BsComplainRO.builder().type(type).handleStatus(handleStatus).build();
        startPage();
        HashMap<String, Object> param = new HashMap<>(2);
        param.put("beginCreateTime", beginCreateTime);
        param.put("endCreateTime", endCreateTime);
        ro.setParams(param);
        List<BsComplain> list = iBsComplainService.queryList(ro);
        return Result.success(getDataTable(list));
    }

    @ApiOperation(value = "投诉管理-详情", tags = {"WEB端-投诉管理"})
    @GetMapping("/{id}")
    public Result<BsComplain> getInfo(@NotNull(message = "主键不能为空")
                                      @PathVariable Long id) {
        return Result.success(iBsComplainService.queryById(id));
    }

    @ApiOperation(value = "投诉管理-批量删除", tags = {"WEB端-投诉管理"})
    @Log(title = "投诉信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result<?> remove(@NotEmpty(message = "主键不能为空")
                            @PathVariable Long[] ids) {
        return Result.toResult(iBsComplainService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    @ApiOperation(value = "投诉管理-投诉处理", tags = {"WEB端-投诉管理"})
    @Log(title = "投诉信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @GetMapping("/handle")
    public Result<?> handle(@Validated(ValidUpdateGroup.class) @RequestBody BsComplainHandleRO ro) {
        ro.setHandleTime(new Date());
        ro.setHandleBy(SecurityUtils.getUsername());
        ro.setHandleStatus(ro.getHandleNeedAgain().equals(ComplainConstants.HANDLE_NEED_AGAIN) ? ComplainConstants.需跟进 : ComplainConstants.已处理);
        BsComplainRO bsComplainRO = new BsComplainRO();
        BeanUtil.copyProperties(ro, bsComplainRO);
        return Result.toResult(iBsComplainService.updateByBo(bsComplainRO));
    }
}
